package cn.rmc.org.service.impl;

import cn.rmc.basic.service.impl.BaseServiceImpl;
import cn.rmc.basic.util.Pagelist;
import cn.rmc.org.domain.Department;
import cn.rmc.org.mapper.DepartmentMapper;
import cn.rmc.org.query.DepartmentQuery;
import cn.rmc.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    @Transactional
    public void add(Department d) {
        String dirpath = buildPath(d);
        d.setDirPath(dirpath);
        departmentMapper.save(d);
    }
    private String buildPath(Department d) {
//      获取当前路径
        System.out.println(d.getParent());
        Department parent = d.getParent();
        String dirpath = "";
//      如果当前路径为空则路径为自己本身id
        if(parent.getId()==null){
            dirpath = "/"+d.getId();
        }else {//父的id作为父路径
            parent = departmentMapper.getParentId(parent.getId());
            dirpath = parent.getDirPath()+"/"+d.getId();
       }
        return dirpath;
    }

    @Override
    @Transactional
    public void upadte(Department d) {
        String dirpath = buildPath(d);
        d.setDirPath(dirpath);
        departmentMapper.update(d);
    }
    @Override
    public List<Department> findDepyTree() {
        return departmentMapper.findDepyTree();
    }
}
